import Vue from 'vue'
import Vuex from 'vuex'
import { setName } from '../utils'
import { login } from "@/api/user"
import { setTheme } from "../theme/theme";
Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    userName: '', // 用户名
    tabsList: [{
      path: '/home',
      name: '首页',
      icon: 'home'
    }],
    currentMenu: null, //高亮显示的tags菜单
    theme: localStorage.getItem("theme") || "default",//主题颜色
  },
  getters: {
    userName: state => state.userName,
    tabsList: state => state.tabsList,
    currentMenu: state => state.currentMenu,
  },
  mutations: {
    SET_NAME: (state, name) => {
      state.userName = name
    },
    setThemeStore(state, theme) {
      setTheme(theme);
    },
    //点击菜单
    selectMenu(state, val) {
      //如果点击的不是首页
      if (val.name !== 'home') {
        state.currentMenu = val;
        //判断点击的菜单是否在列表中
        const res = state.tabsList.findIndex(item => item.name == val.name);
        //如果不在push进去
        if (res == -1) {
          state.tabsList.push(val)
        } else {
          state.currentMenu = null;
        }
      }
    }
  },
  actions: {
    Login({
      commit
    }, userInfo) {
      const name = userInfo.username.trim();
      const password = userInfo.password;
      setName('userName', name);//缓存用户名到cookie
      commit('SET_NAME', name); // 缓存用户信息到vuex

      // login({username : name,password:password}).then(res=>{
      // console.log(res)
      // if(res.code == 1){
      //   this.$router.push("/home");
      // }else{
      //   this.$message.error('登录失败');
      // }
      // })
    },

  },
  modules: {}
})
